package com.bwie.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.pojo.TeamTable;
import com.bwie.pojo.Tourist;
import com.bwie.pojo.Type;
import com.bwie.pojo.User;
import com.bwie.service.TeamTableService;
import com.bwie.service.TouristService;
import com.bwie.service.TypeService;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Auther:${涛}
 * @Date:2024/8/30 14:05
 * @Description: UserController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private TeamTableService teamTableService;

    @Autowired
    private TouristService touristService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @PostMapping("/login")
    public R login(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());

        User userVo = userService.getOne(wrapper);
        if (userVo == null) {
            return R.ERROR(404, "账号错误");
        }

        if (!userVo.getPassword().equals(user.getPassword())) {
            return R.ERROR(404, "密码错误");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie").claim("id", userVo.getId()).claim("roleId", userVo.getRoleId()).compact();
        stringRedisTemplate.opsForValue().set("token:" + userVo.getId(), token, 2, TimeUnit.MINUTES);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("roleId", userVo.getRoleId());

        return R.OK(hashMap);
    }


    //团队列表查询
    @PostMapping("/teamList")
    public R teamList() {
        List<TeamTable> list = teamTableService.list();
        return R.OK(list);
    }

    //游客列表查询
    @PostMapping("/touristList")
    public R touristList() {
        List<Tourist> list = touristService.list();
        return R.OK(list);
    }

    //二级联动
    @PostMapping("/treeList")
    public R treeList() {
        return R.OK(typeService.treeList());
    }

    //查询二级联动
    @PostMapping("/byPid/{id}")
    public R byPid(@PathVariable(name = "id") Integer id) {
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Type::getOrderId, id);

        List<Type> list = typeService.list(wrapper);

        return R.OK(list);
    }

    //游客添加
    @PostMapping("/TouristAdd")
    public R TouristAdd(@RequestBody Tourist tourist) {
        touristService.save(tourist);

        //MQ异步发消息。确保消息不丢失
        String msgId = UUID.randomUUID().toString();
        stringRedisTemplate.opsForValue().set(msgId, tourist.getId() + "");
        rabbitTemplate.convertAndSend("2202-08-30", "", msgId);
        return R.OK();
    }


}


